Ext.define('UX.codeview.SingleCodeviwePopup', {
	extend : 'Ext.window.Window',
	alias : 'widget.singlecodeviwepopup',
	cls : 'codeviewpopup',
	width : 700,
	height : 400,
	modal : true,
	layout : {
		type : 'vbox',
		align : 'stretch'
	},
	title : 'Select dialog',

	initComponent : function() {
		this.items = [ this.buildForm() ];

		this.callParent();

		var grdCodeList = this.down('#grdCodeList');
		var codeviewField = this.codeviewField;

		this.mon(this, {
			show : function() {
				codeviewField.fireEvent('showpopup', codeviewField, this);
				if (this.pStore.autoLoad) {
					this.viewCodeList();
				}
			},
			close : function() {
				codeviewField.fireEvent('closepopup', codeviewField);
				grdCodeList.getStore().removeAll();
			},
			scope : this
		});

		this.mon(grdCodeList, {
			itemClick : function(grid, record, item, index, eOpts) {
				codeviewField.fireEvent('setfieldvalue', codeviewField, record);
				this.close();
			},
			scope : this
		});

		var btnReset = this.down('#btnReset');
		this.mon(btnReset, {
			click : function() {
				codeviewField.fireEvent('clearfieldvalue', codeviewField);
				this.close();
			},
			scope : this
		});

		var btnClearSearchFields = this.down('#btnClearSearchFields');
		this.mon(btnClearSearchFields, {
			click : function() {
				this.clearSearchFields();
				this.viewCodeList();
			},
			scope : this
		});
	},

	viewCodeList : function(reload) {
		var grdCodeList = this.down('#grdCodeList');
		var store = grdCodeList.getStore();
		var params = Ext.applyIf(store.extraParams || {}, this.pParams);
		params = Ext.apply(params, this.getSearchFieldValues())

		store.clearFilter(true);

		/*
		function loadStore(scope) {
			store.load({
				showFail : scope.pShowFailureMsg,
				scope : scope
			});
		}*/

		store.getProxy().extraParams = params;

		store.loadPage(1, {
			showFail : self.pShowFailureMsg,
			scope : self
		});

		/*
		if (this.pCdvType == 'cd' || this.pCdvType == 'table') {
			var conditions = Ext.clone(params.condition) || Ext.clone(this.pCondition);
			conditions = Ext.Array.merge(conditions, this.getSearchFiedValues('conditions'));
			if (conditions.length < 1) {
				delete proxy.extraParams.condition;
			} else {
				proxy.extraParams.condition = conditions;
			}
			loadStore(this);
		} else {
			var filters = this.getSearchFiedValues('filters') || [];
			if (reload || filters.length < 1) {
				loadStore(this);
			} else {
				store.filter(filters);
			}
		}*/
	},
	getSearchFieldValues : function() {
		var cond = {};
		var ctnSearch = this.down('#ctnSearch');
		Ext.Array.each(ctnSearch.items.items, function(searchfield) {
			if (!searchfield.isFormField)
				return;
			cond[searchfield.getName()] = searchfield.getValue();
		});

		return cond;
	},
	getSearchFiedValues : function(type) {
		var ctnSearch = this.down('#ctnSearch');
		var values = [];
		Ext.Array.each(ctnSearch.items.items, function(searchfield) {
			if (!searchfield.isFormField)
				return;
			var value = searchfield.getValue();
			if (value) {
				if (type == 'conditions') {
					values.push({
						column : searchfield.getName(),
						value : value + '%',
						operator : 'like'
					});
				} else if (type == 'filters') {
					values.push({
						property : searchfield.getName(),
						value : value
					});
				}
			}
		}, this);
		return values;
	},

	createSearchFields : function() {
		var searchFields = [];
		var self = this;

		Ext.Array.each(this.pColumns, function(col) {
			if (!col.hidden && !col.disabledSearch) {

				searchFields.push({
					xtype : 'textfield',
					name : col.dataIndex,
					value : col.searchValue || '',
					hideLabel : true,
					emptyText : col.text || col.header,
					flex : col.flex,
					listeners : {
						specialkey : function(textfield, e) {
							if (e.getKey() == e.ENTER) {
								self.viewCodeList();
							}
						}
					}
				});
			}
		}, self);

		return searchFields;
	},

	clearSearchFields : function() {
		var ctnSearch = this.down('#ctnSearch');
		Ext.Array.each(ctnSearch.items.items, function(searchfield) {
			if (!searchfield.isFormField)
				return;
			searchfield.setValue('');
		}, this);
	},

	buildForm : function() {
		var self = this;

		return {
			xtype : 'gridpanel',
			itemId : 'grdCodeList',
			sortableColumns : false,
			enableColumnHide : false,
			enableColumnMove : false,
			columnLines : true,
			flex : 1,
			columns : this.pColumns,
			store : this.pStore,
			dockedItems : [ {
				xtype : 'panel',
				cls : 'windowSearchField',
				dock : 'bottom',
				layout : {
					type : 'hbox',
					align : 'stretch'
				},
				items : [ {
					xtype : 'container',
					itemId : 'ctnSearch',
					items : this.createSearchFields(),
					flex : 1,
					layout : {
						type : 'hbox',
						align : 'stretch'
					}
				}, {
					xtype : 'button',
					itemId : 'btnClearSearchFields',
					iconCls : 'ux-codeview-clearBtn navClearBtn',
					margin : '0 0 0 2',
					tooltip : 'Clear Search Fields'
				} ]
			}, {
				xtype : 'pagingtoolbar',
				itemId : 'barPaging',
				store : this.pStore,
				cls : 'codeviewpagebar',
				displayInfo : true,
				beforePageText : '',
				displayMsg : '{1} / {2}',
				afterPageText : '/ {0}',
				dock : 'bottom',
				doRefresh : function() {
					self.viewCodeList();
				},
				items : [ {
					xtype : 'button',
					itemId : 'btnReset',
					text : 'Fields Reset'
				} ]
			} ]
		};
	}
});